package com.example.administrator.recyclerviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.LinearLayoutManager;
import java.util.ArrayList;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private List<Imagetext> imagetextList=new ArrayList<> ();
    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        recyclerView= (RecyclerView)findViewById(R.id.recycle_view);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        //分割线设置
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL));
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);

        //设置adapter
        recyclerView.setAdapter(adapter = new RecyclerViewAdapter());
    }

    private void init(){
        for(int i=0;i<5;i++) {
            Imagetext a = new Imagetext("aaa", R.mipmap.ic_launcher);
            imagetextList.add(a);
            Imagetext b = new Imagetext("bbb", R.mipmap.ic_launcher);
            imagetextList.add(b);
            Imagetext c = new Imagetext("ccc", R.mipmap.ic_launcher);
            imagetextList.add(c);
            Imagetext d = new Imagetext("ddd", R.mipmap.ic_launcher);
            imagetextList.add(d);
            Imagetext e = new Imagetext("eee", R.mipmap.ic_launcher);
            imagetextList.add(e);
            Imagetext f = new Imagetext("fff", R.mipmap.ic_launcher);
            imagetextList.add(f);
        }
    }

    class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

        class ViewHolder extends RecyclerView.ViewHolder{
            ImageView imageView;
            TextView textView;

            public ViewHolder(View view){
                super(view);
                imageView=(ImageView)view.findViewById(R.id.image);
                textView=(TextView)view.findViewById(R.id.text);
            }
        }

        //创建新View，被LayoutManager所调用
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
            View view=LayoutInflater.from (MainActivity.this)
                    .inflate (R.layout.recyclerview_item,parent,false);
            ViewHolder holder=new ViewHolder(view);
            return holder;
        }

        public void onBindViewHolder(ViewHolder holder,int position){
            Imagetext imagetext=imagetextList.get(position);
            holder.imageView.setImageResource(imagetext.getImageId());
            holder.textView.setText(imagetext.getText());
        }

        public int getItemCount(){
            return imagetextList.size();
        }

    }
}
